const axios = require('axios')
const Log = require('./log')
const logger = new Log()

module.exports = async (config) => {
    const { captchaKey } = await axios({
        url: 'https://sgpt.zjwater.com/oapi/water-manage-auth/api/login/genernateCaptcha',
        method: 'get',
        params: { cKey: (Math.random() * 1000).toFixed(0) }
    }).then(res => res.data.message)

    const { hashKey, publicKey } = await axios({
        url: 'https://sgpt.zjwater.com/oapi/water-manage-auth/api/login/genernateRsaKey',
        method: 'post'
    }).then(res => res.data.message)

    var encrypt = new (require('jsencrypt'))()
    encrypt.setPublicKey(publicKey)
    var encrypted = encrypt.encrypt(config.pwd)

    logger.info('captchaKey:' + captchaKey);
    logger.info('hashKey:' + hashKey);
    logger.info('passWord:' + encrypted);
    logger.info('userName:' + config.user);

    const userInfo = await axios({
        url: 'https://sgpt.zjwater.com/oapi/water-manage-auth/api/login/login',
        method: 'post',
        data: {
            captcha: "",
            captchaKey,
            hashKey,
            passWord: encrypted,
            userName: config.user
        }
    }).then(res => res.data.message)

    logger.info('用户：' + config.user + '登录成功')
    
    const reportDetail = await axios({
        url: 'https://sgpt.zjwater.com/oapi/water-manage-virus/api/fillin/getUserConfirmInfo',
        method: 'post',
        headers: { token: userInfo.token }
    }).then(res => res.data.message)

    await axios({
        url: 'https://sgpt.zjwater.com/oapi/water-manage-virus/api/fillin/saveUserComfirm',
        method: 'post',
        data: reportDetail
    })

    logger.info('用户' + config.user + '填报完成')
}